/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.dataprovider.view.suite.panels.filetable;

import java.io.File;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Kenneth
 */
public class FastaFileTable extends JTable {

    private HashMap<String, File> files = new HashMap<>();

    public FastaFileTable() {
        init();
    }

    public FastaFileTable(File file) {
        init();
        addFile(file);
    }

    public FastaFileTable(Collection<File> files) {
        init();
        addFiles(files);
    }

    public void addFile(File file) {
        this.files.put(file.getName(), file);
    }

    public void addFiles(Collection<File> files) {
        for (File aFile : files) {
            addFile(aFile);
        }
    }

    private void init() {
        setModel(createDefaultTableModel());
        //  addMouseListener(new TableMouseListener(this));
        setComponentPopupMenu(new FastaFileTablePopupMenu(this));
    }

    public void reset() {
        files.clear();
        ((DefaultTableModel) getModel()).setRowCount(0);
    }

    public HashMap<String, File> getFiles() {
        return files;
    }

    public void update() {
        DefaultTableModel model = (DefaultTableModel) getModel();
        for (File aFile : files.values()) {
            model.addRow(getRowContent(aFile));
        }
        int row = model.getRowCount() - 1;
        if (row > -1) {
            setRowSelectionInterval(0, row);
        }
    }

    private String[] getRowContent(File aFile) {
        return new String[]{aFile.getName(), getFileSizeMB(aFile), getFileModDate(aFile)};
    }

    private String getFileSizeMB(File aFile) {
        return String.valueOf(aFile.length() / 1024 / 1024);
    }

    private String getFileModDate(File aFile) {
        return new Date(aFile.lastModified()).toString();
    }

    private DefaultTableModel createDefaultTableModel() {
        DefaultTableModel model = new DefaultTableModel();
        // Create a couple of columns 
        model.addColumn("Filename");
        model.addColumn("Size");
        model.addColumn("Last modified");
        return model;
    }
}
